package com.mangix.conf;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * Created by Ahan on 2014/5/13.
 */
public class MangixConfig {
    private static MangixConfig mangixConfig = null;

    private Properties properties;

    private MangixConfig(Properties properties) {
        this.properties = properties;
    }

    public static synchronized MangixConfig getInstance() throws IOException {
        if (mangixConfig == null) {
            Path configFile = Paths.get("conf/mangix.conf");
            InputStream is = Files.newInputStream(configFile);
            Properties properties = new Properties();
            properties.load(is);
            is.close();
            mangixConfig = new MangixConfig(properties);
            return mangixConfig;
        }
        return mangixConfig;
    }


    public String getHostName() {
        return properties.getProperty("hostname");
    }

    public int getPort() {
        return 8080;
    }

    public int[] getPorts() {
        String[] strPorts = properties.getProperty("port").split(",");
        int[] ports = new int[strPorts.length];
        for (int i = 0; i < strPorts.length; i++) {
            ports[i] = Integer.parseInt(strPorts[i]);
        }
        return ports;
    }

    public int getM() {
        return Integer.parseInt(properties.getProperty("m"));
    }

    public int getCacheSize() {
        return Integer.parseInt(properties.getProperty("cache_size"));
    }

    public String getDBDir() {
        return properties.getProperty("db_dir");
    }

    public String getDataDir() {
        String[] dataDirs = properties.getProperty("data_dir").split("|");
        return dataDirs[0];
    }

    public String[] getDataDirs() {
        System.out.println(properties.getProperty("data_dirs"));
        String[] dataDirs = properties.getProperty("data_dirs").split(",");
        return dataDirs;
    }


    public boolean isMonitor() {
        String strIsMonitor = properties.getProperty("is_monitor", "false");
        return Boolean.parseBoolean(strIsMonitor);
    }

    public int getMonitorPort() {
        return Integer.parseInt(properties.getProperty("monitor_port"));
    }

    public int getMaxReplicasNum(){
        return Integer.parseInt(properties.getProperty("max_replicas_num"));
    }

    public static void main(String[] args) {
        try {
            MangixConfig conf = MangixConfig.getInstance();
            System.out.println(conf.getCacheSize());
            System.out.println(conf.getDBDir());
            System.out.println(conf.getM());
            System.out.println(conf.getHostName());
            String[] dataDirs = conf.getDataDirs();
            System.out.println(dataDirs.length);
            for (String dir : dataDirs) {
                System.out.println(dir);
            }

            System.out.println(conf.isMonitor());
            int[] ports = conf.getPorts();
            for (int i : ports) {
                System.out.println(i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
